<?php
declare (strict_types = 1);
namespace app\api\middleware;

use app\api\service\ForbiddenWordService as ServiceForbiddenWordService;
use think\Request;

class CheckCommentContent
{
    /**
     * 处理请求
     *
     * @param Request $request
     * @param \Closure $next
     * @return Response
     */
    public function handle(Request $request, \Closure $next)
    {
        $commentContent = $request->param('content', '', 'trim');

        $matchedWords = ServiceForbiddenWordService::checkText($commentContent);

        if (!empty($matchedWords)) {
            // 发现违禁词
            return json([
                'code' => 400, // 或其他错误码
                'msg' => '评论内容包含违禁词: ' . implode('、', $matchedWords),
            ]);
        }

        // 没有违禁词，继续处理请求
        return $next($request);
    }
}
